import 'package:flutter/material.dart';

/// 文件类型筛选标签数据
class FileFilterTabData {
  final IconData icon;
  final String label;

  const FileFilterTabData({required this.icon, required this.label});
}

/// 文件类型筛选标签组件
class FilterTabs extends StatelessWidget {
  /// 当前选中的标签索引
  final int currentIndex;

  /// 标签切换回调
  final Function(int) onTabChanged;

  /// 预定义的标签数据
  static const List<FileFilterTabData> tabs = [
    FileFilterTabData(icon: Icons.folder, label: '全部'),
    FileFilterTabData(icon: Icons.image, label: '图片'),
    FileFilterTabData(icon: Icons.video_file, label: '视频'),
    FileFilterTabData(icon: Icons.description, label: '文档'),
  ];

  const FilterTabs({
    Key? key,
    required this.currentIndex,
    required this.onTabChanged,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 50,
      decoration: BoxDecoration(
        color: Theme.of(context).scaffoldBackgroundColor,
        border: Border(
          bottom: BorderSide(color: Theme.of(context).dividerColor, width: 1),
        ),
      ),
      child: ListView.builder(
        scrollDirection: Axis.horizontal,
        itemCount: tabs.length,
        padding: const EdgeInsets.symmetric(horizontal: 8),
        itemBuilder: (context, index) {
          return _buildTabItem(context, index);
        },
      ),
    );
  }

  /// 构建单个标签项
  Widget _buildTabItem(BuildContext context, int index) {
    final tab = tabs[index];
    final isSelected = index == currentIndex;

    return GestureDetector(
      onTap: () => onTabChanged(index),
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 16),
        margin: const EdgeInsets.symmetric(horizontal: 4, vertical: 8),
        decoration: BoxDecoration(
          color:
              isSelected
                  ? Theme.of(context).colorScheme.primaryContainer
                  : Colors.transparent,
          borderRadius: BorderRadius.circular(20),
        ),
        child: Row(
          children: [
            Icon(
              tab.icon,
              size: 16,
              color: isSelected ? Theme.of(context).colorScheme.primary : null,
            ),
            const SizedBox(width: 4),
            Text(
              tab.label,
              style: TextStyle(
                color:
                    isSelected ? Theme.of(context).colorScheme.primary : null,
              ),
            ),
          ],
        ),
      ),
    );
  }

  /// 获取当前选中标签对应的文件类型
  static String getSelectedFileType(int index) {
    switch (index) {
      case 1:
        return 'photo';
      case 2:
        return 'video';
      case 3:
        return 'document';
      default:
        return '';
    }
  }
}
